

package com.tjsiqi.service;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.JbzContractMapper;
import com.tjsiqi.dto.JbzContractDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.JbzContract;
import com.tjsiqi.model.JbzContractEXP;
import com.tjsiqi.param.JbzContractParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysJbzContractService {
    @Autowired
    private JbzContractMapper jbzContractMapper;

    //  查询合同集合
    public PageResult<JbzContract> searchPageList(JbzContractParam jbzContractParam, PageQuery page) {

        JbzContractDto jbzContractDto = getJbzContractDto(jbzContractParam);

        int count = jbzContractMapper.getJbzContractCount(jbzContractDto);

        System.out.println("WeighJblWeighTruckOUTVoCount"+count);
        if (count > 0){
            List<JbzContract> jbzContractList = jbzContractMapper.getPageList(jbzContractDto, page);
            for(JbzContract jbzContract:jbzContractList){
                System.out.println("WeighJblWeighTruckOUTVo"+jbzContract);
            }
            return PageResult.<JbzContract>builder().total(count).data(jbzContractList).build();
        }
        return PageResult.<JbzContract>builder().build();
    }

//  转换查询合同参数
    private static JbzContractDto getJbzContractDto(JbzContractParam jbzContractParam){
        JbzContractDto jbzContractDto = new JbzContractDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (StringUtils.isNotBlank(jbzContractParam.getFromTime())) {
                jbzContractDto.setFromTime(dateFormat.parse(jbzContractParam.getFromTime()));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
                cal.add(Calendar.DATE, -7);
                jbzContractDto.setFromTime(cal.getTime());
                System.out.println("时间kasihi日期"+jbzContractDto.getFromTime());
            }
            if (StringUtils.isNotBlank(jbzContractParam.getToTime())) {
                jbzContractDto.setToTime(dateFormat.parse(jbzContractParam.getToTime()));
            }else{
                jbzContractDto.setToTime(new Date());
                System.out.println("时间jiezhi日期"+jbzContractDto.getToTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd");
        }

        jbzContractDto.setJbzCode(jbzContractParam.getJbzCode());

        return jbzContractDto;
    }

    //导出合同集合
    public List<JbzContractEXP> exportByParam(JbzContractParam jbzContractParam) {

        JbzContractDto jbzContractDto = getJbzContractDto(jbzContractParam);

        List<JbzContractEXP> jbzContractEXPList = jbzContractMapper.exportByParam(jbzContractDto);

        return jbzContractEXPList;
    }
}
